/**
 * @author renbangjie renbangjie@126.com
 * @date 2015年12月9日 下午8:27:26
 * @version V1.0
 */
package com.ai.common.core.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ai.common.core.base.DTO;
import com.ai.common.core.mapper.Mapper;
import com.ai.common.core.pager.model.PagerModel;
import com.ai.common.core.pager.model.PagerUtil;
import com.ai.common.core.pager.service.IPagerService;
import com.ai.common.core.service.IBaseService;
import com.ai.common.exception.AlikAssert;
import com.ai.common.model.Pagination;

/**
 * 业务基类
 *
 * @author renbangjie renbangjie@126.com
 * @date 2015年12月9日 下午8:27:26
 */
public abstract class BaseServiceImpl<T> implements IBaseService<T> {

    /**
     *
     * @date 2015年12月19日 上午11:55:40
     * @return IBaseMapper<T>
     */
    protected abstract Mapper<T> getMapper();

    /*
     * @param primaryKey
     *
     * @return
     *
     * @throws Exception
     *
     * @see com.by.common.core.service.IBaseService#getByPrimaryKey(java.lang.
     * Object)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public T getByPrimaryKey(Object primaryKey) throws Exception {
        return getMapper().getByPrimaryKey(primaryKey);
    }

    /*
     * @param vo
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#getOne(com.by.common.core.base.
     * DTO)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public T getOne(DTO dto) throws Exception {
        return getMapper().getOne(dto);
    }

    /*
     * @param vo
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#getList(com.by.common.core.base.
     * DTO)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<T> getList(DTO dto) throws Exception {
        return getMapper().getList(dto);
    }

    /*
     * @param vo
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#getListCount(com.by.common.core.
     * base.DTO)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Integer getListCount(DTO dto) throws Exception {
        return getMapper().getListCount(dto);
    }

    /*
     * @param paramMap
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#getPagerModel(com.by.common.core.
     * base.DTO)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public PagerModel<T> getPagerModel(DTO dto) throws Exception {
        return PagerUtil.getPagerModel(new IPagerService<T>() {
            @Override
            public int queryCount(DTO dto) throws Exception {
                return getMapper().getListCount(dto);
            }

            @Override
            public List<T> queryList(DTO dto) throws Exception {
                return getMapper().getList(dto);
            }
        }, dto);
    }

    /*
     * @param dto
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#getPagerModel(com.by.common.core.
     * base.DTO)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<T> getPagination(DTO dto) throws Exception {
        return PagerUtil.getPagination(new IPagerService<T>() {
            @Override
            public int queryCount(DTO dto) throws Exception {
                return getMapper().getListCount(dto);
            }

            @Override
            public List<T> queryList(DTO dto) throws Exception {
                return getMapper().getList(dto);
            }
        }, dto);
    }

    /*
     * @param model
     *
     * @return
     *
     * @throws Exception
     *
     * @see com.by.common.core.service.IBaseService#insert(java.lang.Object)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public T insert(T model) throws Exception {
        getMapper().insert(model);
        return model;
    }

    /*
     * @param primaryKey
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#deleteByPrimaryKey(java.lang.
     * Object)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByPrimaryKey(Object primaryKey) throws Exception {
        AlikAssert.isFalse(getMapper().deleteByPrimaryKey(primaryKey) != 1,
                           "删除数据异常");
    }

    /*
     * @param model
     *
     * @return
     *
     * @throws Exception
     *
     * @see
     * com.by.common.core.service.IBaseService#updateByPrimaryKey(java.lang.
     * Object)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByPrimaryKey(T model) throws Exception {
        AlikAssert
                .isFalse(getMapper().updateByPrimaryKey(model) != 1, "更新数据异常");
    }

}
